<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/14
 * Time: 8:58
 */
namespace C\lib{
    require LIB_PATH .'Alipay/AopSdk.php';
    require LIB_PATH. 'Alipay/config.php';
    require LIB_PATH. 'Alipay/lib/AopClient.php';
    require LIB_PATH. 'Alipay/aop/request/AlipayTradeWapPayRequest.php';
    require LIB_PATH. 'Alipay/aop/request/AlipayTradeAppPayRequest.php';
    require LIB_PATH. 'Alipay/aop/request/AlipayOpenPublicTemplateMessageIndustryModifyRequest.php';
    require LIB_PATH. 'Alipay/aop/request/AlipayUserUserinfoShareRequest.php';

    class Zfbpay{
        public  static function  alpay($order,$total_sum){


            $aop = new \AopClient();
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = "2017103009614702";
            $aop->rsaPrivateKey = 'MIIEogIBAAKCAQEAzcCunmS693iSbhZ7eKLwi/32Ayo6JxloXmTa++SobP+lQbI/BF7ONdZUme8HNFiRnqQdl5FrDML3nVrvT/qOfFcTN51EyfomSlPaPpAj9/jztW27QunA+qMAyoyh3Fv2LRl14e8wY+lAvG3+XBuG/OA4h+gYDd5If5OgCCFOyY68egIdFqJzYhq2fyZ3ZHh0iEM3m3gcinOZ+vZcPxMvRmAdHYYYn1mHV5MkH90607YSw9WNPzpxmo+2nvV1paUesZ4PD8+dD10qPmce6K9i4a6P6vw7v25yupKHR/lIuPPU53sFO6rkH/uUktasUzTDJS7MueKy0FxjgjMFG4zAYQIDAQABAoIBACf4BTPZx4QpVPCzx1qA9I+NTwwAWPmdcuvbVbrXCWzbIIQplxpHPHGy6A5bI6AkkG0r3l/CLPR2r1lO/+wedS1sUNxd3a4KvQN1VJEQuRwDrXwSC+2MsSM/JPkz5oGDD8hlCMRSDalq91tjVqUX59oZyIGGMZ4IDzdROyY5Ki/3BDKhuiW7MBSD0c3hO+9+tn/1/3EzJG925drn1SmjcU0m3tczxmjCezBR6VcFvMYzhHTpJ3LQB4MXeGM42lP1XcuLIMx/OXPYRmBKIzBNV6Ce729l9YsO3BlSSxD61BgaJuINR9Jv26GdOjQNRCBoSGlgBQrL505mlDknWcIRXu0CgYEA6Pl6xIB8vs3IwnxisagJ898yE7AkkyTUESj43N5kO/fgrOkjJx7hPg+K5XvQV9HRUZGzWppiIu4LnpZY8TADaPk/K4simrfCihXDLuo/KPz8UCmVWxp5b+yQwXZCUk/kcnkmPH8Cj2niUIt4rY9ObAVEzp4Q9TuqCXv/EGsYMTcCgYEA4hZ1WnzS9o+d5WsLzez11Qq0DOlImuYwh5j9PBsJLZGdvebqpIQhvaHvUS6doY5I1050O5zpolzTpzYsFI9gZxkiQUqP5KkrTRRhgoPTqeC0GQ9qHbu7VkA8Hpefp8m3WajdComMZiLEbpzdGMWe8GXSf2OlyBJBfaQ+UDwBRycCgYApNE6RSIRHns++Dg/KigTQnA4YS1P8rkvPiSaSscaakJmtG0K7XYvK1SbktYRAs/xIZ/OHt7xZny5w6vhqF+rSexrAEciaApwmnfi6aHWpQy1A5EnlJgaKVBW6EbdjJXwq/uBTXMdQJBMB2dkLIUhmdHwZ6vzm0pmdOc//0GEj+wKBgDv+t6+GdjkFjWHm0SAZJHcfOv4+HY01TmV9PJ3pkrrr7E8LavKzOXzn7ui9FQldB4lTAVccYYc7WNMDdhkSnwNc0RFAZwTHu3zdzq5iqxVmUoRf6cnwGltd+lYn6C8PQT9ipx13uLEVwsiTibxcHLwwWPaDC+gWqBheMEb8VV57AoGAcYbTL+RuUA/iVxL7zllKrksXWu8U13vi4lt5yFrH8wfolAiFUYzB1DXsDwPdASkK1SDDHDXVMhTotZ8C9yxCiny4Mwbr6MB5rOpFJBUHozqllMfyO1GmrjiO4/bUZ/Im7Y/mhBQbSyk5cyMWggwNGHtPrrEPaxCMC4qCcS2N/vo=';
            $aop->format = "json";
            $aop->charset = "UTF-8";
            $aop->signType = "RSA2";
            $aop->method= "alipay.trade.app.pay";
            $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqdHXwJVqBzGsCHh0xrjDqxnvXUYDzSmFSvPLoWV+HufQTSh5MB6+xpP8P1c5Dv/CLbcqaWbgwbwx1Lgnxl6Urm3+2QTBLyIsO33968a5uT8j9vHCZEVhUTGXWQ6kG4zaBxdSz5PvHRa1yXFbbvshWB582FZy50FmOyPwCuv0MxHaxb2LMsg2a7UFrNPdOlASr71cTzt5CHtOIriWgjSc+D94gPqlHffa0643/B47xYwZBrhK+bP/kIP4SHLc4MPByLxr2aoXFcWpAyjGA4BsGfaSst4LtvWmNu1iH53JuRR4ZPqIxOHxeZDPQhiy62Q8nD1MR65crBt4dLLz9+LVHQIDAQAB';         //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            $request = new \AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数，这里只需要传入业务参数
            $bizcontent = "{\"body\":\"便递科技押金\","
                . "\"subject\": \"便递科技押金\","
                . "\"out_trade_no\": \"$order\","
                . "\"timeout_express\": \"30m\","
                . "\"total_amount\": \"$total_sum\","
                . "\"product_code\":\"QUICK_MSECURITY_PAY\""
                . "}";
            $request->setNotifyUrl("http://s.biandikeji.com/work/apiPay/notify");
            $request->setBizContent($bizcontent);
//这里和普通的接口调用不同，使用的是sdkExecute
            $response = $aop->sdkExecute($request);
            return $response;
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
           // echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
            //var_dump($response);
            //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
            //return $response;//就是orderString 可以直接给客户端请求，无需再做处理。

        }

        /*
         * 支付宝异步回调
         */
        public static function zfbNotify($data)
        {

            $aop = new \AopClient();

            $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqdHXwJVqBzGsCHh0xrjDqxnvXUYDzSmFSvPLoWV+HufQTSh5MB6+xpP8P1c5Dv/CLbcqaWbgwbwx1Lgnxl6Urm3+2QTBLyIsO33968a5uT8j9vHCZEVhUTGXWQ6kG4zaBxdSz5PvHRa1yXFbbvshWB582FZy50FmOyPwCuv0MxHaxb2LMsg2a7UFrNPdOlASr71cTzt5CHtOIriWgjSc+D94gPqlHffa0643/B47xYwZBrhK+bP/kIP4SHLc4MPByLxr2aoXFcWpAyjGA4BsGfaSst4LtvWmNu1iH53JuRR4ZPqIxOHxeZDPQhiy62Q8nD1MR65crBt4dLLz9+LVHQIDAQAB';

            if ($aop->rsaCheckV1($data,null,"RSA2")) {
                //

                //校验通过
                $trade_status = $_POST['trade_status'];
                if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
                    $arr=[];
                    $arr['order_no'] = $_POST['out_trade_no'];
                    $arr['total_amount'] = $_POST['total_amount'];
                    return $arr;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
}